package com.hr.handler;

import com.hr.dto.Result;
import com.hr.enums.ExceptionEnum;
import com.hr.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = {Exception.class})
    public Result processException(Exception e){
        e.printStackTrace();
        Result result = new Result();
        if(e instanceof MyException){
            MyException ex = (MyException) e;
            result.setCode(ex.getExceptionEnum().getCode());
            result.setMsg(ex.getExceptionEnum().getMsg());
        }else{
            ExceptionEnum ex = ExceptionEnum.ERROR;
            result.setCode(ex.getCode());
            result.setMsg(ex.getMsg());
        }
        return result;
    }

}
